<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}"/>
    <link rel="stylesheet" th:href="@{/res/css/admin.css}"/>
    <script th:src="@{/js/jquery-1.11.3.min.js}"></script>
    <script th:src="@{/myplugs/js/plugs.js}"></script>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" tppabs="http://res.layui.com/layui/dist/css/layui.css"
          media="all">
    <script th:src="@{/layui/layui.js}"></script>

</head>

<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品浏览</strong><small></small></div>
        <div style="float: right">共<span id="total"></span>条记录</div>
    </div>

    <div class="am-g">
        <div class="am-u-sm-12">

                <table class="layui-table">
                    <thead>
                    <tr>
                        <th class="table-title">商品名</th>
                        <th class="table-style">长*宽*高</th>
                        <th class="table-weight">重量(kg)</th>
                        <th class="table-model">型号</th>
                        <th class="table-price">价格</th>
                        <th class="table-warranty_time">保修期</th>
                        <th class="table-set">操作</th>
                    </tr>
                    </thead>

                    <tbody id="mytbody">

                    </tbody>

                </table>
            <div id='page' style="text-align: center"></div>
        </div>
    </div>
</div>
</body>

<script>

    var size = 8;      //每页的数据行
    var total = 0;     //总数据量

    function cutPage() {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage,
                layer = layui.layer;
            //总页数大于页码总数
            laypage.render({
                elem: 'page'
                , count: $("#total").html()
                , first: '首页'
                , last: '尾页'
                , prev: '<em>上一页</em>'
                , next: '<em>下一页</em>'
                , limit: size
                , jump: function (obj) { //jump主要是包含要执行的函数
                    getList(obj.curr)
                }
            })
        })
    }

    //id从url得到
    var result=new Object();
    result=getParameter();
    var uid=result["uid"];



    function getList(pageNum){
        $.get(
            "/goods/getAllGroudingGoodsByPage?pageNum=" + pageNum + "&pageSize=" + size,
            function (data){
                showData(data);
            }
        )
    }

    $(function (){
        $.ajax({
            type:'POST',
            url:'/goods/getAllGroudingGoods',
            dataType:'json',
            success:function (data) {
                total = data.length;
                $("#total").html(total)
                cutPage()
            },
            error:function () {
                alert("error");
            }
        });
    })

    function showData(data) {
        var str= "";
        for(var i = 0; i<data.length; i++){
            str += "<tr class='low-"+ i +"'><td>" + data[i].title + "</td><td>"
            str += data[i].length + "*" + data[i].width + "*" + data[i].height + "</td><td>"
            str += data[i].weight + "</td><td>" + data[i].model + "</td><td>" + data[i].price + "</td><td>" + data[i].warrantyTime  + "</td><td>"
            str += "<button class=\"layui-btn\" onclick='getGoodsId("+data[i].id+")'>"
            str += "<span class=\"am-icon-plus\"></span>添加到商店</button>"
            str += "<button class=\"layui-btn layui-btn-warm\" onclick='addToWish("+data[i].id+")'>"
            str += "<span class=\"am-icon-plus\"></span>添加到心愿单</button>"
            str += "</td></tr>"
        }

        $("#mytbody").html(str);
    };

    function addToWish(id) {
        $.ajax({
            type: "POST",
            url: "/goods/goodsToWish",
            dataType: "text",
            data:{gid: id,uid:uid},
            success:function (data) {
                alert(data);
            },
            error:function (data) {
                alert(data);
            }
        })
    };

    function getGoodsId(id) {
        window.location.href="/goodsToStore?gid="+id+"&uid="+uid;
        window.event.returnValue=false;
    };

    //解析url传值
    function getParameter() {
        var url=location.search;//获取url?后面的内容
        var result=new Object();
        if(url.indexOf("?")!= -1){
            var str=url.substr(1);
            strs=str.split("&");
            for(var i=0;i<strs.length;i++){
                result[strs[i].split("=")[0]]=decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return result;
    }
</script>
</html>